<?php
	class EmployerModel extends Model {
		public function __construct(){
			$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
		}
		public function display(){
			$this->open(Model::MYSQL);
			return $this->getRecords($fields);
		}
		public function findArray($where=array()){
			$this->open(Model::MYSQL);
			return parent::findAsArray($where);
		}
		public function getObject($where=array()) {
			$this->open(Model::MYSQL);
			return parent::getAsObject($where);
		}
		public function getArray($fields=array()){
			$this->open(Model::MYSQL);
			return parent::getAsArray($fields);
		}
		public function findObject($where=array()){
			$this->open(Model::MYSQL);
			return parent::findAsObject($where);
		}
		public function getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
			$this->open(Model::MYSQL);
			return parent::getList($page,$perpage,$search,$searchFields,$sortFields,$sortCriteria);
		}
		public function delete($where=array(),$deleteSet=false){
			$this->open(Model::MYSQL);
			return parent::delete($where,$deleteSet);
		}
		public function numRows($result){
			$this->open(Model::MYSQL);
			return is_resource($result)?mysql_num_rows($result):null;
		}
		public function query( $query ){
			$this->open(Model::MYSQL);
			return parent::query( $query );
		}
		public function save($fields=array()){
			$this->open(Model::MYSQL);
			return parent::save($fields);
		}
		public function update($fields=array(),$where=array()){
			$this->open(Model::MYSQL);
			return parent::update($fields,$where);
		}
		public function getEmployersAsArray(){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Employers ;" ;
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_object($result)){
				$rows[$row->id]=$row;
			}
			return $rows;
		}
		public function getValue($table,$field,$where=array()){
			$this->open(Model::MYSQL);
			return parent::getFieldValue($table,$field,$where);
		}
		public function getEmployerEmail(){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Employers WHERE id IN (1,2,3,4)";
			$result = parent::query($query);
			$employer = array();
			while($rows = mysql_fetch_object($result)){
				$employer[] = $rows;
			}
			return $employer;
		}
		public function checkCanEmail($mail) {
			$this -> open(Model::MYSQL);
			$query = "select email from Employers where email='".$mail."'";
			$result = parent::query($query);
			return mysql_num_rows($result) ? true : false;
		}
		public function checkCanEmailVal($mail) {
			$this -> open(Model::MYSQL);
			$query = "select email from Employers where email='".$mail."'";
			$result = parent::query($query);
			$row=mysql_fetch_object($result);
			return $row->email;
		}
		public function getImgEmployer($emp_id){
			$this->open(Model::MYSQL);
			$query = "SELECT img_id FROM Employers WHERE id='".$emp_id."'";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			
			return $rows->img_id;
		}
		public function getEmailEm(){
			$this->open(Model::MYSQL);
			$query = "SELECT email FROM Employers ;" ;
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_object($result)){
				$rows[]=$row;
			}
			return $rows;
		}
		
		//vitou function
		public function getEmployerReportdisplay($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT *  FROM Employers  ";
		    $query.=" Where DATE_FORMAT(signup_date,'%Y-%m-%d') = '".date("Y-m-d")."' AND username<>'' AND firstname<>'' AND lastname<>'' AND email<>'' AND company_name<>''  " ;
			
			$query.="  ORDER BY signup_date DESC  ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			 $query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		public function getCVReport1($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT  count(NULLIF(rem_attach,'')) as total1 FROM Resumes  ";
			if(!empty($start_date) &&  !empty($end_date)){
				$query.=" Where  DATE_FORMAT(rem_createDate,'%Y-%m-%d')>='".$start_date."' AND DATE_FORMAT(rem_createDate,'%Y-%m-%d')<='".$end_date."' " ;
			}
			$query.=" ORDER BY rem_createDate ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		public function getUserNameEmp($username) {
			$this -> open(Model::MYSQL);
			$query = "SELECT username FROM Employers where username='".$username."'";
			$result = parent::query($query);
			return mysql_num_rows($result) ? true : false;
		} 
		/*start report */
		/* stating of employer created  */
		public function getEmployerReport($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT *  FROM Employers  ";
			if(!empty($start_date) &&  !empty($end_date)){
				$query.=" Where DATE_FORMAT(signup_date,'%Y-%m-%d') >= '".$start_date."' AND DATE_FORMAT(signup_date,'%Y-%m-%d') <='".$end_date."' " ;
			}
			$query.="  ORDER BY signup_date  ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			 $query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		/* ending of employer created  */
		/* stating of candidate created  */
		public function getJobSeekerReport($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT * FROM Candidates  ";
			if(!empty($start_date) &&  !empty($end_date)){
				$query.=" Where  DATE_FORMAT(created,'%Y-%m-%d')>='".$start_date."' AND DATE_FORMAT(created,'%Y-%m-%d')<='".$end_date."' " ;
			}
			$query.=" ORDER BY created ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		/* ending of candidate created  */
		/* stating of CV created  */
		public function getCVReport($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT *  FROM Resumes  ";
			if(!empty($start_date) &&  !empty($end_date)){
				$query.=" Where  DATE_FORMAT(rem_createDate,'%Y-%m-%d')>='".$start_date."' AND DATE_FORMAT(rem_createDate,'%Y-%m-%d')<='".$end_date."' " ;
			}
			$query.=" ORDER BY rem_createDate ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		
		/* ending of CV created  */
		/* starting of  job post   */
		public function getJobPReport($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT *  FROM Jobs  ";
			if(!empty($start_date) &&  !empty($end_date)){
				$query.=" Where  DATE_FORMAT(publish_date,'%Y-%m-%d')>= '".$start_date."' AND DATE_FORMAT(publish_date,'%Y-%m-%d')<='".$end_date."' AND publish ='Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE() " ;
			}
			$query.=" ORDER BY posting_date ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
			
		/* ending of job post   */
		/* start report for making today  */
		public function getEmployerReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Employers WHERE DATE_FORMAT(signup_date,'%Y-%m-%d')='".date("Y-m-d")."' AND username<>'' AND firstname<>'' AND lastname<>'' AND email<>'' AND company_name<>'' ";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		public function getJobSeekerReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Candidates WHERE DATE_FORMAT(created,'%Y-%m-%d')='".date("Y-m-d")."' ";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		public function getResumeAttachmentReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Resumes WHERE DATE_FORMAT(rem_createDate,'%Y-%m-%d')='".date("Y-m-d")."'  AND rem_attach IS NOT NULL AND rem_attach!=''  ";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		public function getResumeNoAttachmentReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Resumes WHERE DATE_FORMAT(rem_createDate,'%Y-%m-%d')='".date("Y-m-d"). "'  AND (rem_attach IS NULL OR rem_attach='') ";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		public function getJobStandardReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Jobs WHERE post_mode='free' AND post_date like '".date("Y-m-d")."%'";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		public function getJobPremiumReportToday(){
			$this->open(Model::MYSQL);
			$query = "SELECT count(*) as total FROM Jobs WHERE post_mode='premium' AND post_date like '".date("Y-m-d")."%'";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		/* by Roth */
		public function getAlljobToday(){
			$this->open(Model::MYSQL);
			$query = "select count(*) as total from Jobs where post_date like '".date("Y-m-d")."%'";
			$result = parent::query($query);
			$rows = mysql_fetch_object($result);
			return $rows;
		}
		/* end report for making today  */
	}
	?>